package com.zhang.sql;

import org.apache.flink.table.functions.AggregateFunction;

/**
 * @title:
 * @author: zhang
 * @date: 2022/2/15 22:54
 */
public class AvgFunc extends AggregateFunction<Double, Avg> {
    @Override
    public Double getValue(Avg accumulator) {
        return (double) accumulator.sum / accumulator.count;
    }

    @Override
    public Avg createAccumulator() {
        return new Avg(0, 0);
    }

    public void accumulate(Avg avg, Integer num) {
        avg.sum = num + avg.sum;
        avg.count += 1;
    }
}
